package com.lb.httpclient;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import java.util.List;
import java.util.Map;

/**
 * @Author: zhangjun
 * @Date: 2020/8/7 14:17
 * @Description: httpclient
 */
public class ClientTest {
    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        //用户中心
        /*String result = doGet("https://gateway.vocabgo.com/Student/Main?timestamp=1596779627154&versions=1.1.0");
        System.out.println(result);
        ObjectMapper objectMapper = new ObjectMapper();
        Map map =  objectMapper.readValue(result, Map.class);
        Map data = MapUtils.getMap(map,"data");
        List<Map> list = (List<Map>) MapUtils.getObject(data, "menu_list");
        System.out.println(MapUtils.getMap(map,"data"));
        list.forEach(i -> {
            System.out.println("菜单名称 >>> " + MapUtils.getString(i,"label") + ", 菜单路径 >>> " + MapUtils.getString(i,"router"));
        });*/

        //获取任务
        /*String task = doGet("https://gateway.vocabgo.com/Student/ClassTask/List?page_count=1&page_size=10&timestamp=1596783099310&versions=1.1.0");
        System.out.println(task);*/

        //题目
        String url = "https://gateway.vocabgo.com/Student/ClassTask/StartAnswer?task_id=16951627&task_type=2&release_id=533666&opt_img_w=822&opt_font_size=44&opt_font_c=%23000000&it_img_w=968&it_font_size=49&timestamp=1596865248970&versions=1.1.0 HTTP/1.1";
        String userToken = "e57d465f47e56b138267fb79209e9d6c";
        String question = CDRUtils.doGet(url,userToken);

        Map map =  objectMapper.readValue(question, Map.class);
        Map data = MapUtils.getMap(map,"data");
        String topicCode = MapUtils.getString(data,"topic_code");
        System.out.println(" question >>>> " + question);
        System.out.println(" topicCode >>>> " + topicCode);

        //确认答案
        AnswerDTO answerDTO = new AnswerDTO();
        answerDTO.setAnswer(2);
        answerDTO.setTopic_code(topicCode);
        answerDTO.setTimestamp(System.currentTimeMillis());
        answerDTO.setVersions("1.1.0");
        answerDTO.setSign("185ef6d50397bb6955b2bb40f23c0854");
        String params = objectMapper.writeValueAsString(answerDTO);

        List<Header> headers = Lists.newArrayList();
        headers.add(new BasicHeader("Host","gateway.vocabgo.com"));
        headers.add(new BasicHeader("Connection","keep-alive"));
        headers.add(new BasicHeader("Accept","application/json, text/plain, */*"));
        headers.add(new BasicHeader("Origin","https://app.vocabgo.com"));
        headers.add(new BasicHeader("UserToken","eef210997071be32b6f31db68ad2f164"));
        headers.add(new BasicHeader("X-Requested-With","XMLHttpRequest"));
        headers.add(new BasicHeader("User-Agent","Mozilla/5.0 (Linux; Android 10; SM-G9730 Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2575 MMWEBSDK/200701 Mobile Safari/537.36 MMWEBID/3845 MicroMessenger/7.0.17.1720(0x27001137) Process/toolsmp WeChat/arm64 NetType/WIFI Language/zh_CN ABI/arm64"));
        headers.add(new BasicHeader("Content-Type","application/json;charset=UTF-8"));
        headers.add(new BasicHeader("Sec-Fetch-Site","same-site"));
        headers.add(new BasicHeader("Sec-Fetch-Mode","Sec-Fetch-Mode"));
        headers.add(new BasicHeader("Referer","https://app.vocabgo.com/overall/"));
        headers.add(new BasicHeader("Accept-Encoding","gzip, deflate"));
        headers.add(new BasicHeader("Accept-Language","zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"));

        String verifyAnswer = CDRUtils.doPost("https://gateway.vocabgo.com/Student/ClassTask/VerifyAnswer", params, headers);
        Map verifyMap =  objectMapper.readValue(verifyAnswer, Map.class);
        Map verifyData = MapUtils.getMap(verifyMap,"data");
        String verifyTopicCode = MapUtils.getString(verifyData,"topic_code");

        System.out.println(" >>> " + verifyAnswer);
        System.out.println(" verifyTopicCode >>>> " + verifyTopicCode);

        // 提交保存答案
        SaveAnswerDTO saveAnswerDTO = new SaveAnswerDTO();
        saveAnswerDTO.setTopic_code(verifyTopicCode);
        saveAnswerDTO.setTime_spent(5800);//答题耗时
        saveAnswerDTO.setIt_img_w(888);//字段不详
        saveAnswerDTO.setIt_font_size(44);//来源不详
        saveAnswerDTO.setOpt_font_c("#000000");//来源不详
        saveAnswerDTO.setOpt_font_size(49);//来源不详
        saveAnswerDTO.setTimestamp(System.currentTimeMillis());
        saveAnswerDTO.setVersions("1.1.0");
        saveAnswerDTO.setSign("");//来源不详

        String saveParams = objectMapper.writeValueAsString(answerDTO);

        String nextQuestion = CDRUtils.doPost("https://gateway.vocabgo.com/Student/ClassTask/VerifyAnswer", saveParams, headers);
        Map nextQuestionMap =  objectMapper.readValue(nextQuestion, Map.class);
        Map nextQuestionData = MapUtils.getMap(nextQuestionMap,"data");
        Map stem = MapUtils.getMap(nextQuestionData,"stem");
        List<Map> options = (List<Map>) MapUtils.getMap(nextQuestionData,"options");
        String nextQuestionTopicCode = MapUtils.getString(nextQuestionData,"topic_code");

        System.out.println(" nextQuestion >>> " + nextQuestion);
        System.out.println(" nextQuestionTopicCode >>>> " + nextQuestionTopicCode);
        System.out.println(" stem >>>> " + stem);
        System.out.println(" options >>>> " + options);


    }
}
